package cn.edu.zime.tjh.iotapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.zime.tjh.iotapp.R;
import cn.edu.zime.tjh.iotapp.model.Device;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {
    private static final String TAG = "DeviceAdapter";

    private Context context;
    private List<Device> deviceList;
    private OnDeviceClickListener listener;

    // 点击回调接口
    public interface OnDeviceClickListener {
        void onDeviceClick(Device device);

        void onDeviceLongClick(Device device, int position);
    }

    public DeviceAdapter(Context context, List<Device> deviceList, OnDeviceClickListener listener) {
        this.context = context;
        this.deviceList = deviceList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        Device device = deviceList.get(position);

        // 设置设备基本信息
        holder.textDeviceName.setText(device.getDevice_name());

        // 设置设备类型，如果存在
        if (device.getDevice_type() != null && !device.getDevice_type().isEmpty()) {
            holder.textDeviceType.setText(device.getDevice_type());
            holder.textDeviceType.setVisibility(View.VISIBLE);
        } else {
            holder.textDeviceType.setVisibility(View.GONE);
        }

        // 设置在线状态
        if (device.isOnline()) {
            holder.textStatus.setText("在线");
            holder.textStatus.setTextColor(context.getResources().getColor(R.color.status_online));
        } else {
            holder.textStatus.setText("离线");
            holder.textStatus.setTextColor(context.getResources().getColor(R.color.status_offline));
        }

        // 设置设备信息（如果有）
        if (device.getDevice_info() != null && !device.getDevice_info().isEmpty()) {
            holder.textLastActive.setText(device.getDevice_info());
            holder.textLastActive.setVisibility(View.VISIBLE);
        } else {
            holder.textLastActive.setVisibility(View.GONE);
        }

        // 移除位置信息显示，因为API中没有这个字段
        holder.textLocation.setVisibility(View.GONE);

        // 设置点击事件
        holder.cardDevice.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeviceClick(device);
            }
        });

        // 设置长按事件
        holder.cardDevice.setOnLongClickListener(v -> {
            if (listener != null) {
                listener.onDeviceLongClick(device, position);
                return true;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return deviceList.size();
    }

    // 添加设备
    public void addDevice(Device device) {
        deviceList.add(device);
        notifyItemInserted(deviceList.size() - 1);
    }

    // 移除设备
    public void removeDevice(int position) {
        if (position >= 0 && position < deviceList.size()) {
            deviceList.remove(position);
            notifyItemRemoved(position);
        }
    }

    // ViewHolder类
    static class DeviceViewHolder extends RecyclerView.ViewHolder {
        CardView cardDevice;
        TextView textDeviceName;
        TextView textDeviceType;
        TextView textStatus;
        TextView textLastActive;
        TextView textLocation;

        DeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            cardDevice = itemView.findViewById(R.id.cardDevice);
            textDeviceName = itemView.findViewById(R.id.textDeviceName);
            textDeviceType = itemView.findViewById(R.id.textDeviceType);
            textStatus = itemView.findViewById(R.id.textStatus);
            textLastActive = itemView.findViewById(R.id.textLastActive);
            textLocation = itemView.findViewById(R.id.textLocation);
        }
    }
} 